const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
console.log('==BUILD_YF==>', process.env.BUILD_YF)
console.log('==env,', process.env.NODE_ENV)
console.log('====url==', process.env.VUE_APP_BASE_API)
let dist = ''
let getApi = ''
// 打包目录的配置
switch (process.env.BUILD_YF) {
    case 'dev':
        // 开发 dev
        dist = 'devDist'
        getApi = 'http://devjd.zp.com'
        break
    case 'pro':
        // 开发测试分支 testDev
        // 测试  devDist
        dist = 'dist' // testDist
        getApi = 'http://jd.com'
        break
}
module.exports = {
    publicPath: './',
    // 输出文件目录
    outputDir: `./${dist}`, //./${dist}/${config.version}
    // assetsDir: `assets${version}`,
    // 取消 eslint 验证
    lintOnSave: 'error',
    filenameHashing: false,
    productionSourceMap: false,
    configureWebpack: config => {
        const plugins = []
        if (process.env.NODE_ENV === 'production') {
            // 生产环境去 注释 console
            plugins.push(
                new UglifyJsPlugin({
                    uglifyOptions: {
                        output: {
                            comments: true // 去掉注释
                        },
                        warnings: true,
                        compress: {
                            drop_console: true,
                            drop_debugger: true,
                            pure_funcs: ['console.log'] //移除console
                        }
                    }
                })
            )
            // 取消webpack警告的性能提示
            config.performance = {
                hints: 'warning',
                //入口起点的最大体积
                maxEntrypointSize: 1000 * 500,
                //生成文件的最大体积
                maxAssetSize: 1000 * 1000,
                //只给出 js 文件的性能提示
                assetFilter: function (assetFilename) {
                    return assetFilename.endsWith('.js')
                }
            }
            config.externals = {
                Vue: 'Vue',
                Vuex: 'Vuex',
                VueRouter: 'VueRouter'
            }
            config.mode = 'production'
            config['performance'] = {
                //打包文件大小配置
                maxEntrypointSize: 10000000,
                maxAssetSize: 30000000
            }
            config['optimization'] = {
                splitChunks: {
                    chunks: 'all',
                    automaticNameDelimiter: '-'
                }
            }
        }
        return { plugins }
    },
    chainWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            // 给js和css配置版本号
            config.output.filename(`./js/[name].[chunkhash:8].js`).end()
            config.output.chunkFilename(`./js/[name].[chunkhash:8].js`).end()
            config.plugin('extract-css').tap(args => [
                {
                    filename: `./css/[name].[contenthash:8].css`,
                    chunkFilename: `./css/[name].[contenthash:8].css`
                }
            ])
        }
        config.plugin('html').tap(args => {
            args[0].title = '国网供应链金融全寿命周期动态利率盘'
            return args
        })
    },
    devServer: {
        port: 3031, // 端口
        disableHostCheck: true,
        open: false, // 自动启动浏览器
        // host: "demo.zhenpin.com", // localhost
        // https: false,
        hotOnly: false, // 热更新
        proxy: {
            [process.env.VUE_APP_BASE_API]: {
                target: getApi, // 重写路径 http://test.zhenpin.com/  //http://back.dev.zp.com
                // ws: true,   //开启WebSocket
                // secure: false,      // 如果是https接口，需要配置这个参数
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.VUE_APP_BASE_API]: '' // 利用这个地面的值拼接上target里面的地址
                }
            }
        }
    }
}
